import pygame

from PyPixelCore.Setting import *
from PyPixelCore.block.BaseBlock import BaseBlock


def simplify_update(self):
    pass


def base_draw(self, surf):  # 需要改进
    surf.blit(self.image, self.rect)

    if show_rect_collision:
        # pygame.draw.rect(surf, Color_type.RED, self.rect, width=3)
        pygame.draw.rect(surf, Color_type.BLACK, self.collision_rect, width=collision_surface_width)
        pygame.draw.rect(surf, Color_type.RED, self.rect, width=block_surface_width)

        # pygame.draw.circle(surf, Color_type.RED, self.rect.center, 50, width=2)
        # pygame.draw.line(surf, Color_type.RED, self.rect.bottomleft, self.rect.bottomright, width=4)
        # pygame.draw.polygon(surf, Color_type.RED, [(1, 1), (27, 90), (111, 111)], width=3)
        # pygame.draw.arc(surf, Color_type.RED, self.rect, 0, 5, width=100)
        # pygame.draw.aalines(surf, Color_type.RED, False, [(1, 1), (27, 90), (111, 111)], blend=1)


"""
    Block --> BaseBlock
    比较完整的自定义绘制块 或 图片块
    可直接使用或者继承优化
    功能:
        显示图像或者自定义绘制
        支持碰撞检测
        支持帧函数与计时器函数
        如果需要加载图片仅支持加载静态图片
"""


class Block(BaseBlock):
    def __init__(self, width=100, height=100, bk_color=Color_type.BLACK, point_x=0, point_y=0, layer=0, collision=False, is_alpha=False):
        super().__init__(layer)
        self.draw_func = base_draw
        self.timer_arr = []
        self.update_func = simplify_update
        self.timer_update_func = simplify_update
        self.bk_color = bk_color

        base_surface = pygame.Surface((width, height))
        base_surface.fill(bk_color)  # 用黑色填充
        self.image = base_surface

        self._type = Block_type.block  # 绘制成块
        self.rect = pygame.Rect(point_x, point_y, width, height)  # 为了节省运行时间,更新的时候需要自行更新
        self.is_collision = collision
        self.collision_rect = self.rect.copy()
        self.is_alpha = is_alpha

    def reload_image(self):
        if self.is_dirty:
            base_surface = pygame.Surface((self.rect.width, self.rect.height))
            base_surface.fill(self.bk_color)  # 用背景填充
            self.image = base_surface
            self.is_dirty = False

    def draw(self, surf):
        self.draw_func(self, surf)

    def update(self):
        self.reload_image()
        # 帧函数的调用
        if self.update_func:
            self.update_func(self)
        # 帧计时器函数的调用
        if self.timer_arr:
            for timer_group in self.timer_arr:
                timer_group[0].next_frame()
                if timer_group[0].is_end:
                    timer_group[1](self)
                    if timer_group[0].type == 0:  # 一次性循环则删除不参与reset
                        self.timer_arr.remove(timer_group)
                    else:
                        timer_group[0].reset()


    def set_collision_rect(self, width, height):
        self.collision_rect = pygame.Rect(0, 0, width, height)
        self.collision_rect.center = self.rect.center
        self.is_dirty = True

    def set_collision_rect_use_offset(self, offset_x, offset_y, width, height):
        self.collision_rect = pygame.Rect(self.rect.x + offset_x, self.rect.y + offset_y, width, height)
        self.is_dirty = True

    def set_frame_animation(self, func):
        self.update_func = func

    def set_timer_animation(self, timer, func):
        self.timer_arr.append([timer, func])

    def delete_timer_animation(self, func):
        for each_func in self.timer_arr:
            if each_func[1] == func:
                self.timer_arr.remove(each_func)

    def set_draw_function(self, func):
        self.draw_func = func

    def clean_timer(self):
        self.timer_arr = []

    def load_image(self, url, size_lock=False):
        if self.is_alpha:
            self.image = pygame.image.load(url).convert_alpha()
        else:
            self.image = pygame.image.load(url)
        if size_lock:
            self.image = pygame.transform.scale(self.image, (self.rect.width, self.rect.height))


